package com.adwin.addcoin.ui.dialog

import android.content.Context
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.widget.Toast
import com.adwin.addcoin.base.BaseDialog
import com.adwin.addcoin.databinding.DialogUserAgreementBinding
import com.adwin.addcoin.utils.dataStore
import com.adwin.addcoin.utils.saveAgreement
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class UserAgreementDialog(val mContext: Context): BaseDialog(mContext) {
    private val oneTitle =
            "We take your personal information and privacyprotection very seriously.ln order to betterprotect your personal rights and interests.before you use our products,please carefullyread all the terms in the "
    private val clickString = "《Privacy Policy》"
    val title = oneTitle+clickString+" especially:\n" +
            "1.Our collection of your personal information/Preservation/use/external provision/protectionand other rules and terms,as well as your userrights and other terms;\n" +
            "2.Agreement on our limited liability andexemption clauses;"

    lateinit var binding: DialogUserAgreementBinding

    override fun getGravity(): Int = Gravity.CENTER

    override fun getWidth(): Int  = LayoutParams.MATCH_PARENT

    override fun getHeight(): Int = LayoutParams.MATCH_PARENT

    override fun initLayout(): View {
        binding = DialogUserAgreementBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun onStart() {
        super.onStart()
    }

    override fun initData() {
        val stringBuilder = SpannableStringBuilder(title)
        val span = object :ClickableSpan(){

            override fun updateDrawState(ds: TextPaint) {
                ds.color = Color.parseColor("#416AC0");
                //设置是否有下划线
                ds.isUnderlineText = true;
            }

            override fun onClick(widget: View) {
                PrivacyPolicyDialog(mContext).show()
            }
        }
        stringBuilder.setSpan(span,oneTitle.length,oneTitle.length+clickString.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.tvUserAgreementMessage.text = stringBuilder
        binding.tvUserAgreementMessage.movementMethod = LinkMovementMethod.getInstance()
        binding.tvUserAgreeQuit.setOnClickListener {
            dismiss()
        }
        binding.tvUserAgreeOk.setOnClickListener {
            MainScope().launch(Dispatchers.IO) {
                context.dataStore.saveAgreement(true)
                withContext(Dispatchers.Main){
                    dismiss()
                }
            }
        }
    }
}